/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  org.apache.camel.component.custom.CustomConsumer
 * 
 *   Date:   2014-3-3
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.developers.component;

import java.util.concurrent.ExecutorService;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultConsumer;

import com.titan.demo.camel.utils.LogUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class CustomConsumer extends DefaultConsumer implements Runnable {
    private ExecutorService executor;
    private CustomEndpoint endpoint;
    
    
    public CustomConsumer(CustomEndpoint endpoint, Processor processor, String uri) {
        super(endpoint, processor);
        this.endpoint = endpoint;
        LogUtils.log(CustomConsumer.class, "CustomConsumer", "uri="+uri);
    }

    @Override
    protected void doStart() throws Exception {
        super.doStart();
        LogUtils.log(CustomConsumer.class, "doStart");        

        executor = endpoint.getCamelContext().getExecutorServiceManager().newSingleThreadExecutor(this, endpoint.getEndpointUri());
        executor.execute(this);
    }

    @Override
    public void doStop() throws Exception {
        if (executor != null) {
            endpoint.getCamelContext().getExecutorServiceManager().shutdownNow(executor);
            executor = null;
        }
        LogUtils.log(CustomConsumer.class, "doStop");
        super.doStop();
    }
    
    public void run() {
        LogUtils.log(CustomConsumer.class, "run");
        try {
            System.out.println("输入参数.name="+endpoint.getName());

            Exchange exchange = endpoint.createExchange();
            Message in = exchange.getIn();
            in.setBody("body_in_data");
            in.setHeader("head1", "head1_in_data");
            in.setHeader("head2", "head2_in_data");
            in.setHeader("name", endpoint.getName());
            
            getProcessor().process(exchange);
            
            Message out = exchange.getOut();
            System.out.println("out.body="+out.getBody());
        } catch (Exception e) {
            getExceptionHandler().handleException(e);
        }        
    }

}

